Home |
| Latest | About | Random
# Brief review of `struct` and constructors. **(READ: Section 15.3)** ## A `struct` with two member variables called `Point`. Let us say we are creating a `struct` as a data type for `Point`, where it records the `x` and `y` coordinates as `double`s as **member variables.** So we might write something like this ```cpp #include using namespace std; struct Point { double x, y; }; int main() { return 0; } ``` . . . . . ## Without a constructor, assign by hand. To declare a `Point` called `a`, we can do ```cpp Point a; ``` - Note: at this point `a.x` and `a.y` are **uninitialized**. (They hold garbage values!) And we can assign to the member variables `x` and `y` for `Point a` by doing ```cpp a.x = 3; a.y = 4; ``` for example, so now the `Point` `a` has coordinates `(x, y) = (3, 4)`. Assigning each member one at a time works. A **constructor** does better: it initializes the member variables at the moment a is constructed. . . . . . ## Writing a constructor. We modify inside the **interface** of the `struct` for `Point`. A constructor for `Point` is the function `Point::Point`. ```cpp #include using namespace std; struct Point { double x, y; Point () {} // this is a constructor! }; int main() { return 0; } ``` This constructor is called the moment a `Point` is created, where the code inside the function body would be executed. Check this out. ```cpp #include using namespace std; struct Point { double x, y; // this is a constructor! Point () { cout << "a point is made!" << endl; } }; int main() { Point a; return 0; } ``` . . . . . ## Initialize the members as the `Point` is built ```cpp #include using namespace std; struct Point { double x, y; Point (double _x, double _y) : x(_x), y(_y) { cout << "a point is made!" << endl; } }; int main() { return 0; } ``` Here we use a **member initializer list** (the parts that says `: x(_x), y(_y)`) to initialize each member variable `x` and `y` with the values of the parameters `_x` and `_y`. - `x(_x)` means initialize the member variable `x` with the value held by parameter `_x`. - `y(_y)` means initialize the member variable `y` with the value held by parameter `_y`. . . . . . ## Something to watch out for. Note the following. - The name of this constructor for this `struct` `Point` is actually `Point::Point`. Note the constructor name has to match the `struct` name. - The constructor has no return type, not even `void`! If you write `void` as a return type for a constructor, it would be a **build error!** Curiously, you can still use the `return` keyword in a constructor to end the constructor call early. - If you have a user-defined constructor, the compiler will no longer generate a default constructor. If the user wants one, the user has to define a default constructor themselves. - That is, in our example, if one is to call `Point a;`, then we would get a build error since we defined our own constructor with two parameters and no default constructor is explicitly defined. . . . . . ## What happens when we call the constructor. Now if we call the constructor `Point::Point(double _x, double _y)`, with arguments `3,4`: ```cpp Point a(3, 4); ``` Then the following thing happens in order: - (1) A space in memory is reserved for a `Point` named `a`, that holds the member variables `x` and `y`. - (2) A function scope is created. - (3) An **implicit parameter** called `self` is created in this function scope, and it "references" the **implicit argument** `a`. (Technically it is a pointer, but let us treat it as a reference for now as in the book). - (4) The **explicit parameters** `_x` and `_y` are created in this function scope. And the explicit arguments are passed in. - (5) Next we process the **member initializer list** `: x(_x), y(_y)` in this case. This means to.. - Initialize the member variable `x` with the value of the parameter `_x` - Initialize the member variable `y` with the value of the parameter `_y` - We know what `x` and `y` are because we can access it through `self`! - (6) Execute the code in the function body `{ }`. - Destroy the scope. . . **Draw a diagram!** ![[1-teaching/ucla-2026-fall-pic-10a/---files/brief-overview-of-constructor 2026-11-18 06.00.47.excalidraw.svg]] %%[[1-teaching/ucla-2026-fall-pic-10a/---files/brief-overview-of-constructor 2026-11-18 06.00.47.excalidraw.md|🖋 Edit in Excalidraw]]%% . . . . . ## Overloading and bringing back the default constructor. As mentioned, our `Point` does not have a default constructor anymore, as we have a user-defined constructor. **The default constructor is one with no parameters**. So we can create our own default constructor, that **overloads** our constructor: ```cpp #include using namespace std; struct Point { double x, y; Point (double _x, double _y) : x(_x), y(_y) { cout << "a point is made!" << endl; } Point () : x(0), y(0) {} // default constructor with no parameters }; int main() { Point a(3,4); // using constructor Point::Point(double, double) Point o; // default constructed return 0; } ``` - Careful: write `Point o;`, **not** `Point o();`. The latter does not create a `Point`! C++ reads it as declaring a *function* named `o` that takes no arguments and returns a `Point`.